package com.be.beadmin.basicData.impl;


import com.be.beadmin.basicData.entity.Role;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.basicData.repository.UserRepository;
import com.be.beadmin.common.entity.SecurityRoleDto;
import com.be.beadmin.common.entity.SecurityUserDto;

import com.be.beadmin.common.service.SecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author chenwei
 * @date 2/24/2023 3:52 PM
 */
@Service
public class SecurityUserServiceImpl implements SecurityUserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserServiceImpl userService;

    @Override
    public SecurityUserDto findUserById(String id) {

//        private Boolean online = false;
//        private Date lastAccessTime;
//        private Boolean allowMulti = false;
//        private String token;

        User user = userRepository.findUserByIdAndDelete(id,0);

        // 保证默认测试
        SecurityUserDto securityUserDto = new SecurityUserDto();
        Set<SecurityRoleDto> securityRoleDtoSet = new HashSet<>();

        securityUserDto.setId(user.getId());
        securityUserDto.setName(user.getName());
        securityUserDto.setPassword(user.getPassword());
        securityUserDto.setLastAccessTime(user.getLastAccessTime());
        securityUserDto.setToken(user.getToken());
        securityUserDto.setAllowMulti(true);
        Set<Role> roles = user.getRoles();
        roles.forEach(item -> {
            SecurityRoleDto securityRoleDto = new SecurityRoleDto();
            securityRoleDto.setId(item.getId());
            securityRoleDto.setCode(item.getCode());
            securityRoleDto.setName(item.getName());
            securityRoleDtoSet.add(securityRoleDto);
            securityUserDto.setRoles(securityRoleDtoSet);
        });

        return securityUserDto;
    }

    @Override
    public SecurityUserDto getUser(String name) {
        User user = userService.getUser(name);
        // 保证默认测试添加测试
        SecurityUserDto securityUserDto = new SecurityUserDto();
        Set<SecurityRoleDto> securityRoleDtoSet = new HashSet<>();
//        securityUserDto.setId("402881ed854de8d201854de911030000");
//        securityUserDto.setName("admin");
//        // 明文密码对应 1234
//        securityUserDto.setPassword("$2a$10$f9SW0Rsf09KPCHbIer6Dt.g6DycQeh1wSrMT3Koxdilf0XrVvTmSm");
//        Set<SecurityRoleDto> securityRoleDtoSet = new HashSet<>();
//        SecurityRoleDto securityRoleDto = new SecurityRoleDto();
//        securityRoleDto.setCode("ROLE_Admin");
//        securityRoleDto.setName("系统管理员");
//        securityRoleDtoSet.add(securityRoleDto);
//        securityUserDto.setRoles(securityRoleDtoSet);
        securityUserDto.setId(user.getId());
        securityUserDto.setName(user.getName());
        securityUserDto.setPassword(user.getPassword());
        securityUserDto.setLastAccessTime(user.getLastAccessTime());
        securityUserDto.setAllowMulti(true);
        securityUserDto.setToken(user.getToken());
        Set<Role> roles = user.getRoles();
        roles.forEach(item -> {
            SecurityRoleDto securityRoleDto = new SecurityRoleDto();
            securityRoleDto.setId(item.getId());
            securityRoleDto.setCode(item.getCode());
            securityRoleDto.setName(item.getName());
            securityRoleDtoSet.add(securityRoleDto);
            securityUserDto.setRoles(securityRoleDtoSet);
        });

        return securityUserDto;
    }
}
